﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.ETL.Properties;

namespace Workflows.Components.ETL
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class BFileUploadComponent:WorkflowComponentBase
    {
        #region props
        private BFileUploadInstruction _Instruction;
        private BFileUploadOutput _Output;

        public override InstructionBase Instruction
        {
            get { return _Instruction; }
            set { _Instruction =(BFileUploadInstruction) value; }
        }

        public override ResultBase Output
        {
            get { return _Output; }
            set { _Output =(BFileUploadOutput) value; }
        }

        /// <summary></summary>
        public string SrcFolderPath
        {
            get
            {
                return this._Instruction.SrcFolderPath;
            }
            set
            {
                this._Instruction.SrcFolderPath = value;
            }
        }

        /// <summary></summary>
        public bool IncludeSubFolder
        {
            get
            {
                return this._Instruction.IncludeSubFolder;
            }
            set
            {
                this._Instruction.IncludeSubFolder = value;
            }
        }

        /// <summary></summary>
        public string Filter
        {
            get
            {
                return this._Instruction.Filter;
            }
            set
            {
                this._Instruction.Filter = value;
            }
        }

        /// <summary></summary>
        public string ConnStr
        {
            get
            {
                return this._Instruction.ConnStr;
            }
            set
            {
                this._Instruction.ConnStr = value;
            }
        }

        /// <summary></summary>
        public string TableName
        {
            get
            {
                return this._Instruction.TableName;
            }
            set
            {
                this._Instruction.TableName = value;
            }
        }

        /// <summary></summary>
        public string FileNameField
        {
            get
            {
                return this._Instruction.FileNameField;
            }
            set
            {
                this._Instruction.FileNameField = value;
            }
        }

        /// <summary></summary>
        public string FilePathField
        {
            get
            {
                return this._Instruction.FilePathField;
            }
            set
            {
                this._Instruction.FilePathField = value;
            }
        }

        /// <summary></summary>
        public string ImageFieldName
        {
            get
            {
                return this._Instruction.ImageFieldName;
            }
            set
            {
                this._Instruction.ImageFieldName = value;
            }
        }

        /// <summary></summary>
        public string KeyFieldName
        {
            get
            {
                return this._Instruction.KeyFieldName;
            }
            set
            {
                this._Instruction.KeyFieldName = value;
            }
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        public BFileUploadComponent():base()
        {
            this.ComponentName = "Upload BFile";
            this.Description = "Upload file contents from file system to database";
            this.ComponentIcon = Resources.RawSource_201;
            this.ComponentImage = Resources.backup;
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.ETL.ToString();

            this._Instruction =
                new BFileUploadInstruction(this.ComponentID, Guid.Empty, string.Empty, "*.*", true, string.Empty,
                                           string.Empty, string.Empty, string.Empty, string.Empty, string.Empty);
            this._Output = new BFileUploadOutput(this.ComponentID, Guid.Empty, 0, string.Empty, string.Empty);

        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SrcFolderPath", this.SrcFolderPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Filter", this.Filter);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IncludeSubFolder", this.IncludeSubFolder.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnStr", this.ConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableName", this.TableName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileNameField", this.FileNameField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FilePathField", this.FilePathField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ImageField", this.ImageFieldName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "KeyField", this.KeyFieldName);

            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                BFileUploadComponent wfComponent = base.Instantiate(xNode) as BFileUploadComponent;

                wfComponent.SrcFolderPath = xNode.Attributes.GetNamedItem("SrcFolderPath").Value;
                wfComponent.Filter = xNode.Attributes.GetNamedItem("Filter").Value;
                wfComponent.IncludeSubFolder = bool.Parse(xNode.Attributes.GetNamedItem("IncludeSubFolder").Value);
                wfComponent.ConnStr = xNode.Attributes.GetNamedItem("ConnStr").Value;
                wfComponent.TableName = xNode.Attributes.GetNamedItem("TableName").Value;
                wfComponent.FileNameField = xNode.Attributes.GetNamedItem("FileNameField").Value;
                wfComponent.FilePathField = xNode.Attributes.GetNamedItem("FilePathField").Value;
                wfComponent.ImageFieldName = xNode.Attributes.GetNamedItem("ImageField").Value;
                wfComponent.KeyFieldName = xNode.Attributes.GetNamedItem("KeyField").Value;

                return wfComponent;
            }
            else
            {
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
            }
        }
    }
}
